/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package as24gc.parser;

/**
 *
 * @author nico.vassallo
 */
import as24gc.db.Dealers;
import as24gc.db.Vehicles;
import java.util.Date;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class EngineParserSax extends DefaultHandler {

    private String currentElement;
    private Vehicles v = new Vehicles();
    private Dealers dealers = new Dealers();
    private String action;
    private InputObject inputObject;


    public EngineParserSax() {
    }

    @Override
    public void startElement(String namespaceURI, String sName, String qName, Attributes attrs)
            throws SAXException {
        //System.out.println("Start Element: " + qName);
        this.setCurrentElement(qName);
        //this.getTupleId(attrs);
    }

    @Override
    public void characters(char buf[], int offset, int len)
            throws SAXException {
        //System.out.println("len: " + len);
        String c = new String(buf, offset, len);
        c = c.trim();
        int lenC = c.length();
        //System.out.println("lenC: " + lenC);
        if (lenC >= 1) {
            //System.out.println("characters: " + c);
            
            
            if (this.currentElement.equalsIgnoreCase("nid")) {
                if (c.length() >= 1)
                this.getV().setNid(Integer.parseInt(c));
                System.out.println("set nid");
            }
            if (this.currentElement.equalsIgnoreCase("azione")) {
                this.setAction(c);
                System.out.println("set v");
            }

            if (this.currentElement.equalsIgnoreCase("dealer_id")) {
                this.getDealers().setId(Integer.parseInt(c));
                System.out.println("set dealer_id");
            }

            if (this.currentElement.equalsIgnoreCase("username")) {
                this.getDealers().setUser(c);
                System.out.println("set username");
            }


            if (this.currentElement.equalsIgnoreCase("password")) {
                this.getDealers().setPassword(c);
                System.out.println("set password");
            }


            if (this.currentElement.equalsIgnoreCase("cover")) {
                v.setMedia(c);
                System.out.println("set cover");
            }

            if (this.currentElement.equalsIgnoreCase("imgs")) {
                v.setMedia(v.getMedia() + ";" + ParserUtility.parserWorkAroundMemcar(c));
                System.out.println("set media");
            }

            if (this.currentElement.equalsIgnoreCase("produttore")) {
                v.setBrand(c);
                System.out.println("set brand");
            }

            if (this.currentElement.equalsIgnoreCase("modello")) {
                v.setModel(c);
                System.out.println("set Model");
            }

            if (this.currentElement.equalsIgnoreCase("versione")) {
                v.setVersion(c);
                System.out.println("set versione");
            }

            if (this.currentElement.equalsIgnoreCase("tipologia")) {
                v.setCategory(c);
                System.out.println("setCategory");
            }

            if (this.currentElement.equalsIgnoreCase("cambio")) {
                v.setGearType(c);
                System.out.println("setGearType");
            }


            // body type in xslx file
            if (this.currentElement.equalsIgnoreCase("carrozzeria")) {
                v.setBody(c);
                System.out.println("set carrozzeria.");
            }

            if (this.currentElement.equalsIgnoreCase("colore_esterni")) {
                v.setBodyColor(c);
                System.out.println("set colore_esterni");
            }

            if (this.currentElement.equalsIgnoreCase("colore_interni")) {
                v.setBodyColorgroup(c);
                System.out.println("set colore_interni");
            }

            if (this.currentElement.equalsIgnoreCase("metallizzata")) {
                v.setBodyPainting(c);
                System.out.println("setBodyPainting");
            }

            if (this.currentElement.equalsIgnoreCase("carburante")) {
                v.setFuelType(c);
                System.out.println("set carburante");
            }

            if (this.currentElement.equalsIgnoreCase("immatricolazione")) {
                Date a = ParserUtility.parserDate(c);
                v.setInitialRegistration(a);
                System.out.println("set immatricolazione");
            }

            if (this.currentElement.equalsIgnoreCase("prezzo")) {
                v.setPrices(c);
                System.out.println("set prezzo");
            }

            if (this.currentElement.equalsIgnoreCase("chilometraggio")) {
                int km = ParserUtility.parserKm(c);
                v.setMileage(km);
                System.out.println("set chilometraggio");
            }

            if (this.currentElement.equalsIgnoreCase("potenza")) {
                int kw = ParserUtility.parserKw(c);
                v.setKilowatt(kw);
                System.out.println("set kilowatt");
            }

            if (this.currentElement.equalsIgnoreCase("note")) {
                v.setNote(c);
                System.out.println("set note");
            }

        }

    }

    /*
    private void getTupleId(Attributes attrs)
    {
    if (attrs != null) {
    
    for (int i = 0; i < attrs.getLength(); i++) {
    String aName = attrs.getLocalName(i); // Attr name
    System.out.println("attrsName: " +attrs.getQName(i));
    System.out.println("attrsValue: " +attrs.getValue(i));
    if ("".equals(aName)){
    
    
    if (currentElement.equalsIgnoreCase("TSOheader"))
    if (i==0)
    this.setValue_TSOID(attrs.getValue(i));
    else 
    this.setValue_TSOlabel(attrs.getValue(i));
    
    }
    }
    }
    }
     */
    @Override
    public void endElement(String namespaceURI,
            String sName, // simple name
            String qName // qualified name
            )
            throws SAXException {
        System.out.println("End Element: " + qName);
        if (qName.equalsIgnoreCase("as24gcbody")) {
            this.getV().setDealers(this.getDealers());
            this.inputObject = new InputObject(this.getV(), this.getAction());
        }
        

    }

    /**
     * @return Returns the currentElement.
     */
    public String getCurrentElement() {
        return currentElement;
    }

    /**
     * @param currentElement The currentElement to set.
     */
    private void setCurrentElement(String currentElement) {
        this.currentElement = currentElement;
    }
    
    
    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public Dealers getDealers() {
        return dealers;
    }

    public void setDealers(Dealers dealers) {
        this.dealers = dealers;
    }

    public Vehicles getV() {
        return v;
    }

    public void setV(Vehicles v) {
        this.v = v;
    }
    
    public InputObject getInputObject() {
        return inputObject;
    }

    public void setInputObject(InputObject inputObject) {
        this.inputObject = inputObject;
    }

}
